import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { ItemEntity, ItemType, ItemRarity } from "../entities/item.entity";

@Injectable()
export class ItemService {
  constructor(
    @InjectRepository(ItemEntity)
    private itemRepository: Repository<ItemEntity>
  ) {}

  /**
   * 根据ID获取物品
   */
  async getItemById(id: number): Promise<ItemEntity | null> {
    return await this.itemRepository.findOne({
      where: { id, status: 1 }
    });
  }

  /**
   * 根据类型获取物品列表
   */
  async getItemsByType(itemType: ItemType, pageNum: number = 0, pageSize: number = 20): Promise<{
    list: ItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.itemRepository.findAndCount({
      where: { itemType, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" }
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize)
    };
  }

  /**
   * 获取商店物品列表
   */
  async getShopItems(pageNum: number = 0, pageSize: number = 20): Promise<{
    list: ItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.itemRepository.findAndCount({
      where: { isOnSale: 1, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" }
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize)
    };
  }

  /**
   * 获取积分商城物品列表
   */
  async getPointShopItems(pageNum: number = 0, pageSize: number = 20): Promise<{
    list: ItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.itemRepository.findAndCount({
      where: { isOnPointShop: 1, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" }
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize)
    };
  }

  /**
   * 根据稀有度获取物品列表
   */
  async getItemsByRarity(rarity: ItemRarity, pageNum: number = 0, pageSize: number = 20): Promise<{
    list: ItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.itemRepository.findAndCount({
      where: { rarity, status: 1 },
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" }
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize)
    };
  }

  /**
   * 搜索物品
   */
  async searchItems(keyword: string, pageNum: number = 0, pageSize: number = 20): Promise<{
    list: ItemEntity[];
    total: number;
    pageNum: number;
    pageSize: number;
    totalPages: number;
  }> {
    const [items, total] = await this.itemRepository.findAndCount({
      where: [
        { name: `%${keyword}%`, status: 1 },
        { description: `%${keyword}%`, status: 1 }
      ],
      skip: pageNum * pageSize,
      take: pageSize,
      order: { id: "ASC" }
    });

    return {
      list: items,
      total,
      pageNum,
      pageSize,
      totalPages: Math.ceil(total / pageSize)
    };
  }

  /**
   * 创建物品
   */
  async createItem(itemData: Partial<ItemEntity>): Promise<ItemEntity> {
    const item = this.itemRepository.create(itemData);
    return await this.itemRepository.save(item);
  }

  /**
   * 更新物品
   */
  async updateItem(id: number, itemData: Partial<ItemEntity>): Promise<ItemEntity | null> {
    await this.itemRepository.update(id, itemData);
    return await this.getItemById(id);
  }

  /**
   * 删除物品（软删除）
   */
  async deleteItem(id: number): Promise<boolean> {
    const result = await this.itemRepository.update(id, { status: 0 });
    return (result.affected || 0) > 0;
  }

  /**
   * 获取所有物品类型
   */
  getItemTypes(): { value: number; label: string }[] {
    return [
      { value: ItemType.MEDICINE, label: "药品" },
      { value: ItemType.POWER_POTION, label: "精力药水" },
      { value: ItemType.TREASURE, label: "宝物" },
      { value: ItemType.EXPERIENCE_BOOK, label: "经验书" },
      { value: ItemType.KEY, label: "钥匙" },
      { value: ItemType.BOX, label: "宝盒" },
      { value: ItemType.TOKEN, label: "令牌" },
      { value: ItemType.OTHER, label: "其他" }
    ];
  }

  /**
   * 获取所有稀有度
   */
  getItemRarities(): { value: number; label: string }[] {
    return [
      { value: ItemRarity.COMMON, label: "普通" },
      { value: ItemRarity.UNCOMMON, label: "优秀" },
      { value: ItemRarity.RARE, label: "稀有" },
      { value: ItemRarity.EPIC, label: "史诗" },
      { value: ItemRarity.LEGENDARY, label: "传说" }
    ];
  }
} 